package com.qwc.blog.controller.back;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qwc.blog.common.constant.CommonConstant;
import com.qwc.blog.common.utils.TreeUtils;
import com.qwc.blog.common.validate.IdValid;
import com.qwc.blog.entity.Category;
import com.qwc.blog.entity.vo.TreeSelect;
import com.qwc.blog.entity.vo.category.CategoryDto;
import com.qwc.blog.security.service.LoginService;
import com.qwc.blog.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.qwc.blog.common.constant.ResponseConstant.DATA;

/**
 * <p>
 * 文章分类表 前端控制器
 * </p>
 *
 * @author qwc
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/blog/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private LoginService loginService;

    @GetMapping("/list")
    public ResponseEntity<Object> list(String name, String status) {
        Map<String, Object> map = new HashMap<>(4);
        List<Category> data = categoryService.selectCategoryList(name, status);
        map.put(DATA, data);
        return ResponseEntity.ok(map);
    }

    @PostMapping
    public ResponseEntity<Object> addCategory(@Validated @RequestBody CategoryDto dto) {
        categoryService.addCategory(dto.createCategory(), loginService.getCurrentUsername());
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public ResponseEntity<Object> updateCategory(@Validated(IdValid.class) @RequestBody CategoryDto dto) {
        categoryService.updateCategory(dto.createCategory(), loginService.getCurrentUsername());
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{categoryId}")
    public ResponseEntity<Object> getCategory(@PathVariable @NotNull(message = "分类Id不能为空") Integer categoryId) {
        Map<String, Object> map = new HashMap<>(4);
        Category category = categoryService.getById(categoryId);
        map.put(DATA, category);
        return ResponseEntity.ok(map);
    }

    @DeleteMapping("/{categoryId}")
    public ResponseEntity<Object> deleteCategory(@PathVariable @NotNull(message = "分类Id不能为空") Integer categoryId) {
        categoryService.deleteById(categoryId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/treeSelect")
    public ResponseEntity<Object> treeSelect() {
        Map<String, Object> map = new HashMap<>(4);
        List<Category> list = categoryService.list(Wrappers.<Category>lambdaQuery()
                .eq(Category::getStatus, CommonConstant.YES)
                .orderByAsc(Arrays.asList(Category::getPid, Category::getSort)));
        List<TreeSelect> collect = list.stream().map(category -> {
            TreeSelect treeSelect = new TreeSelect();
            treeSelect.setId(category.getCategoryId());
            treeSelect.setLabel(category.getName());
            treeSelect.setPid(category.getPid());
            return treeSelect;
        }).collect(Collectors.toList());
        List<TreeSelect> data = TreeUtils.buildTree(collect);
        map.put(DATA, data);
        return ResponseEntity.ok(map);
    }

}
